//
//  NewsListCell.m
//  History
//
//  Created by jam on 16/9/4.
//  Copyright © 2016年 kane. All rights reserved.
//

#import "NewsListCell.h"

@implementation NewsListCell
@synthesize titleLabel,detailLabel,photoView,deleteBtn;
- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)changeNightMode
{
    self.backgroundView = nil;
    self.backgroundColor = KKColor_Black_White;
    self.contentView.backgroundColor = KKColor_Black_White;
    self.titleLabel.textColor = KKColor_White_Black;
    self.detailLabel.textColor = KKColor_Black_LightGary;
    self.photoView.backgroundColor = KKColor_Black_White;
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];
}


- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuseIdentifier];
    if (self) {
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(changeNightMode) name:kChangeNightMode object:nil];

        titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 10, 200, 40)];
        titleLabel.backgroundColor = [UIColor clearColor];
        titleLabel.font = [UIFont systemFontOfSize:17];
        [self.contentView addSubview:titleLabel];
        
        detailLabel = [[UILabel alloc] initWithFrame:CGRectMake(0,  10, 110, 20)];
        detailLabel.backgroundColor = [UIColor clearColor];
        detailLabel.font = [UIFont systemFontOfSize:12];
        [self.contentView addSubview:detailLabel];
        
        photoView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 100, 100)];
        [self.contentView addSubview:photoView];
        
        deleteBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        deleteBtn.frame = CGRectMake(0, 0,40, 30);
        [deleteBtn setImage:[UIImage imageNamed:@"Home_delete"] forState:UIControlStateNormal];
        [deleteBtn setImageEdgeInsets:UIEdgeInsetsMake(7, 5, 7, 5)];
        [deleteBtn addTarget:self action:@selector(deleteBtnPressed:) forControlEvents:UIControlEventTouchUpInside];
        [self.contentView addSubview:deleteBtn];
        [self changeNightMode];
    }
    return self;
}

- (void)setupNewsData:(NSDictionary *)news
{
    NSString *imgString = [news[@"a_picture"] convertToString];
    NSArray *photoAry = [imgString componentsSeparatedByString:@","];
    NSArray *imgList = @[];
    if (photoAry.count >= 3) {
        imgList = [NSArray arrayWithObjects:photoAry[0],photoAry[1],photoAry[2], nil];
    }else if (photoAry.count >= 1){
        imgList = [NSArray arrayWithObjects:photoAry[0], nil];
    }
    titleLabel.text = news[@"a_title"];
    NSString *timeString = [[NSDate dateWithTimeIntervalSince1970:[news[@"a_createtime"] longLongValue]] timeIntervalDescription];
    NSString *detail = [NSString stringWithFormat:@"%@ %@",[news[@"source"] convertToString],timeString];
    NSInteger commCount = [[news[@"a_commentcount"] convertToString] integerValue];
    if (commCount > 0) {
        detail = [NSString stringWithFormat:@"%@ %@评 %@",[news[@"source"] convertToString],[news[@"a_commentcount"] convertToString],timeString];
    }
    detailLabel.text = detail;
    NSInteger imgCount = imgList.count;
    switch (imgCount) {
        case 0:
        {
            titleLabel.frame = CGRectMake(10, 5, self.contentView.width-20, 40);
            detailLabel.frame = CGRectMake(10, titleLabel.maxY+10, 110, 20);
            deleteBtn.frame = CGRectMake(self.contentView.width-45, titleLabel.maxY+10, 30, 30);
            
        }
            break;
        case 1:
        {
            titleLabel.frame = CGRectMake(10, 5, 190, 40);
            titleLabel.numberOfLines = 2;
            [titleLabel sizeToFit];
            detailLabel.frame = CGRectMake(10, titleLabel.maxY+10, 110, 20);
            deleteBtn.frame = CGRectMake(180, detailLabel.minY-5, 30, 30);
            photoView.frame = CGRectMake(self.contentView.width - 90 - 10, titleLabel.minY, 90, 70);
            
        }
            break;
        case 3:
        {
            titleLabel.frame = CGRectMake(10, 5, self.contentView.width-20, 40);
            titleLabel.numberOfLines = 2;
            [titleLabel sizeToFit];
            photoView.frame = CGRectMake(10, titleLabel.maxY+10, self.contentView.width-20, 80);
            detailLabel.frame = CGRectMake(photoView.minX, photoView.maxY + 10, 110, 20);
            deleteBtn.frame = CGRectMake(self.contentView.width-45, photoView.maxY+1, 30, 30);
        }
            break;
        default:
            break;
    }
    
    dispatch_async(dispatch_get_main_queue(), ^{
        [self setupPhotos:imgList];
    });
}

- (void)setupPhotos:(NSArray *)photos
{
    [photoView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
    photoView.hidden = NO;
    switch (photos.count) {
        case 0: {
            photoView.hidden = YES;
        } break;
        case 1: {
            UIImageView *photoImage = [[UIImageView alloc] initWithFrame:photoView.bounds];
            [photoImage setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@",FileURL,photos.firstObject]] placeholderImage:[UIImage imageNamed:@"defaultImage"]];
            [photoView addSubview:photoImage];
        }  break;
        case 3: {
            for (int i = 0; i < 3; i++) {
                UIImageView *photoImage = [[UIImageView alloc] initWithFrame:CGRectMake(0+(94+4)*i, 0, 94, 80)];
                [photoImage setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@",FileURL,photos[i]]] placeholderImage:[UIImage imageNamed:@"defaultImage"]];
                [photoView addSubview:photoImage];
            }
        } break;
        default:
            break;
    }
}



- (void)deleteBtnPressed:(UIButton *)button
{
    UIWindow* window = [UIApplication sharedApplication].keyWindow;
    CGRect rect1 = [button convertRect:button.frame fromView:self.contentView];     //获取button在contentView的位置
    CGRect rect2 = [button convertRect:rect1 toView:window];              //获取button在window的位置
        
    if (_delegate && [_delegate respondsToSelector:@selector(clickFilterButton:withCGRect:)]) {
        [_delegate clickFilterButton:_dataIndex withCGRect:rect2];
    }
}

@end
